package com.yyz.lease.web.admin.service.impl;

import com.wf.captcha.SpecCaptcha;
import com.yyz.lease.common.constant.RedisConstant;
import com.yyz.lease.common.exception.LeaseException;
import com.yyz.lease.common.result.ResultCodeEnum;
import com.yyz.lease.common.utils.JwtUtil;
import com.yyz.lease.model.entity.SystemUser;
import com.yyz.lease.model.enums.BaseStatus;
import com.yyz.lease.web.admin.service.LoginService;
import com.yyz.lease.web.admin.service.SystemUserService;
import com.yyz.lease.web.admin.vo.login.CaptchaVo;
import com.yyz.lease.web.admin.vo.login.LoginVo;
import com.yyz.lease.web.admin.vo.system.user.SystemUserInfoVo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {
    private final StringRedisTemplate redisTemplate;
    private final SystemUserService systemUserService;

    /**
     * 获取图形验证码
     */
    @Override
    public CaptchaVo getCaptcha() {

        SpecCaptcha specCaptcha = new SpecCaptcha(130, 48, 4);

        String code = specCaptcha.text().toLowerCase();
        String key = RedisConstant.ADMIN_LOGIN_PREFIX + UUID.randomUUID();
        String image = specCaptcha.toBase64();
        redisTemplate.opsForValue().set(key, code, RedisConstant.ADMIN_LOGIN_CAPTCHA_TTL_SEC, TimeUnit.SECONDS);

        return new CaptchaVo(image, key);
    }

    /**
     * 登录验证并返回jwt令牌
     */
    @Override
    public String login(LoginVo loginVo) {
        //验证码为空
        if (loginVo.getCaptchaCode() == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_NOT_FOUND);
        }

        String code = redisTemplate.opsForValue().get(loginVo.getCaptchaKey());
        //验证码已过期
        if (code == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_EXPIRED);
        }
        //验证码错误
        if (!code.equals(loginVo.getCaptchaCode().toLowerCase())) {
            throw new LeaseException(ResultCodeEnum.ADMIN_CAPTCHA_CODE_ERROR);
        }

        SystemUser user = systemUserService.getByUsername(loginVo.getUsername());
        //账号不存在
        if (user == null) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_NOT_EXIST_ERROR);
        }

        //账号被禁用
        if (user.getStatus() == BaseStatus.DISABLE) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_DISABLED_ERROR);
        }

        //账号或密码错误
        if (!user.getPassword().equals(DigestUtils.md5Hex(loginVo.getPassword()))) {
            throw new LeaseException(ResultCodeEnum.ADMIN_ACCOUNT_ERROR);
        }

        return JwtUtil.createToken(user.getId(),user.getUsername());
    }

    /**
     * 根据id获取用户信息
     */
    @Override
    public SystemUserInfoVo getUserInfo(Long id) {
        SystemUser user = systemUserService.getById(id);
        SystemUserInfoVo systemUserInfoVo = new SystemUserInfoVo();
        systemUserInfoVo.setName(user.getName());
        systemUserInfoVo.setAvatarUrl(user.getAvatarUrl());

        return systemUserInfoVo;
    }
}
